package Queuedemo;
//循环队列
public class MyCircularQueue {

    public int front;//队头下标
    public int rear;//队尾下标
    public int[] elem;

    //构造方法,k为队列长度
    public MyCircularQueue(int k) {
        this.elem = new int[k];//实际有效空间为k-1
        //this.elem = new int[k+1];//oj题写法或者使用标记/数组大小变量
    }
    //入队
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        this.elem[this.rear] = value;
        this.rear = (this.rear+1)%this.elem.length;
        return true;
    }
    //出队
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        this.front = (this.front+1)%this.elem.length;
        return true;
    }
    //获取队头元素
    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return this.elem[this.front];
    }
    //获取队尾元素
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int index = -1;
        if(this.rear == 0){
            index = this.elem.length-1;
        }else{
            index = this.rear-1;
        }
        return this.elem[index];
    }
    //是否为空
    public boolean isEmpty() {
        return this.front == this.rear;
    }
    //是否为满
    public boolean isFull() {
        if(this.front == (this.rear+1)%this.elem.length){
            return true;
        }
        return false;
    }
}
